using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class SkillTrigger_UnitDie : SkillTrigger
    {
        protected override ESkillTriggerType triggerType => ESkillTriggerType.UnitDie;

        private ETargetRelation relation = ETargetRelation.None;

        protected override void OnInitialize(Skill rSkill, TriggerConfig rConfig)
        {
            base.OnInitialize(rSkill, rConfig);
            if (rConfig.customConfig is SkillTrigger_UnitDie_ConfigData rConfigData)
            {
                this.relation = (ETargetRelation)rConfigData.relation;
            }
        }


        protected override void OnEnable()
        {
            base.OnEnable();
            this.AddListener<Unit, Unit>(EBattleTriggerType.Battle_UnitDie, this.OnEvent);
        }

        private void OnEvent(Unit dieUnit, Unit attacker)
        {
            if (this.relation == ETargetRelation.None || (this.relation & BattleUtil.CheckRelation(this.executer, dieUnit)) != 0)
            {
                var rVirInput = ClassPool.Get<SelectParam>();
                rVirInput.unit = this.skill.executer;
                rVirInput.pos = rVirInput.unit.pos;
                this.OnTrigger(rVirInput);
            }
        }

        public override void OnReset()
        {
            base.OnReset();
            this.relation = ETargetRelation.None;
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }

#if UNITY_EDITOR
        public override string ToString()
        {
            return $"{this.relation}单位死亡时";
        }
#endif
    }
}